package com.huanke.iot.api.service.device.basic;

import com.alibaba.fastjson.JSONObject;
import com.huanke.iot.api.controller.h5.req.ChildDeviceRequest;
import com.huanke.iot.api.gateway.MqttSendService;
import com.huanke.iot.base.dao.customer.CustomerMapper;
import com.huanke.iot.base.dao.device.DeviceMapper;
import com.huanke.iot.base.dao.device.ability.DeviceAbilityMapper;
import com.huanke.iot.base.dao.device.typeModel.DeviceModelAbilityMapper;
import com.huanke.iot.base.dao.device.typeModel.DeviceModelMapper;
import com.huanke.iot.base.dao.device.typeModel.DeviceTypeMapper;
import com.huanke.iot.base.dao.format.WxFormatMapper;
import com.huanke.iot.base.po.device.DevicePo;
import com.huanke.iot.base.po.device.typeModel.DeviceModelPo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述:
 *
 * @author onlymark
 * @create 2018-09-11 上午10:00
 */
@Repository
@Slf4j
public class DeviceIotService {
    @Autowired
    private DeviceHighService deviceHighService;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceModelMapper deviceModelMapper;


    public JSONObject childDeviceList(Integer hostDeviceId) {
        List<Map<String, Object>> childList = deviceMapper.selectIotChildList(hostDeviceId);
        JSONObject jb = new JSONObject();
        JSONObject childJb = new JSONObject();
        for (Map<String, Object> childDevice : childList) {
            childJb.put((String) childDevice.get("childId"), childDevice.get("modelId").toString());
        }
        jb.put("childlst", childJb);
        return jb;
    }

    public JSONObject config(String modelNo) {

        DeviceModelPo deviceModel = deviceModelMapper.selectByModelNo(modelNo);
        String helpFileUrl = deviceModel.getHelpFileUrl();
        if(StringUtils.isEmpty(helpFileUrl)){
            helpFileUrl = "";
        }
        String[] fileList = helpFileUrl.split(",");
        JSONObject jb = new JSONObject();
        jb.put(modelNo, fileList);
        return jb;
    }
    @Transactional
    public Boolean updateChildDevice(Integer hostDeviceId, JSONObject updateObject) throws Exception {
        List<DevicePo> devicePos = deviceMapper.selectChildDeviceListByHostDeviceId(hostDeviceId);
        List<String> childIdList = devicePos.stream().map(devicePo -> devicePo.getChildId()).collect(Collectors.toList());
//        JSONObject updateObject = JSONObject.parseObject(updateObj.toString());
        JSONObject set = updateObject.getJSONObject("set");
        Set<String> updateChildIdList = set.keySet();
        for (String childId : updateChildIdList) {
            JSONObject newChild = set.getJSONObject(childId);
            if(childIdList.contains(childId)){
                //update
                ChildDeviceRequest request = new ChildDeviceRequest();
                request.setHostDeviceId(hostDeviceId);
                request.setDeviceName(newChild.getString("alias"));
                request.setChildId(childId);
                request.setModelId(newChild.getInteger("dev_type"));
                deviceHighService.updateChildDevice(request);

            }else{
                //new
                ChildDeviceRequest request = new ChildDeviceRequest();
                request.setHostDeviceId(hostDeviceId);
                request.setDeviceName(newChild.getString("alias"));
                request.setChildId(childId);
                request.setModelId(newChild.getInteger("dev_type"));
                deviceHighService.addChildDevice(request);
            }
        }
        //新的没有，旧的有。delete
        List<Integer> delChildIdList = childIdList.stream().filter(e -> !updateChildIdList.contains(e)).map(e->Integer.valueOf(e)).collect(Collectors.toList());
        for (Integer delChildId : delChildIdList) {
            deviceHighService.iotdeleteById(delChildId);
        }
        return true;
    }
}
